﻿using MyCommon;
using MyIServices;
using MyModels.Models;
using MyServices.BASE;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyServices
{
    public class RoleService : BaseService<Role>, IRoleService
    {
        [Caching]
        public async Task<string> GetRoleNameById(long roleId)
        {
            var role = await QueryById(roleId);
            return role?.Name!;
        }

        public async Task<Role> SaveRole(string roleName)
        {
            var role = new Role(roleName);
            var roleModule = new Role();
            var userList = await Query(a => a.Name == role.Name && a.Enabled);
            if (userList.Count > 0) {
                roleModule = userList.FirstOrDefault();
            }
            else
            {
                var id = await Add(role);
                roleModule = await QueryById(id);
            }
            return roleModule;
        }
    }
}
